﻿using System.Collections;
using UnityEngine;

// 小人
public class Man: Box {
    private Sprite mCurrentImage;           // 当前图片
    private Sprite mCurrentImage2;          // 当前图片2
    private Sprite mImageLeft1;             // 左图片1
    private Sprite mImageLeft2;             // 左图片2
    private Sprite mImageRight1;            // 右图片1
    private Sprite mImageRight2;            // 右图片2
    private Sprite mImageUp1;               // 上图片1
    private Sprite mImageUp2;               // 上图片2
    private WaitForSeconds mWaitForDance;   // 跳舞两个动作之间的间隔时间

    /**
     * 跳舞协程。
     * @return 跳舞两个动作之间的间隔时间
     */
    private IEnumerator Dancing() {
        while (true) {
            if (mRenderer.sprite == mCurrentImage) {
                mRenderer.sprite = mCurrentImage2;
            } else {
                mRenderer.sprite = mCurrentImage;
            }
            yield return mWaitForDance;
        }
    }

    /**
     * 移动。
     * @param direction     方向
     * @param moveResult    移动结果
     * @return 被推动的箱子
     */
    public override BaseGameObject Move(KeyCode direction, ref MoveResult moveResult) {
        Vector2 origin = Vector2.zero;
        RaycastHit2D raycastHit2D = new RaycastHit2D();
        float destX = transform.position.x, destY = transform.position.y;   // 新位置的坐标

        // 根据方向，计算新位置的坐标和图片
        switch (direction) {
            case KeyCode.DownArrow:
                origin = new Vector2(transform.position.x, transform.position.y - Game.Pivot.y - Const.RAY_LENGTH);
                raycastHit2D = Physics2D.Raycast(origin, Vector2.down, Const.RAY_LENGTH);
                destY--;
                mCurrentImage = mImage;
                mCurrentImage2 = mImage2;
                break;
            case KeyCode.LeftArrow:
                origin = new Vector2(transform.position.x - Game.Pivot.x - Const.RAY_LENGTH, transform.position.y);
                raycastHit2D = Physics2D.Raycast(origin, Vector2.left, Const.RAY_LENGTH);
                destX--;
                mCurrentImage = mImageLeft1;
                mCurrentImage2 = mImageLeft2;
                break;
            case KeyCode.RightArrow:
                origin = new Vector2(transform.position.x + Game.Pivot.x + Const.RAY_LENGTH, transform.position.y);
                raycastHit2D = Physics2D.Raycast(origin, Vector2.right, Const.RAY_LENGTH);
                destX++;
                mCurrentImage = mImageRight1;
                mCurrentImage2 = mImageRight2;
                break;
            case KeyCode.UpArrow:
                origin = new Vector2(transform.position.x, transform.position.y + Game.Pivot.y + Const.RAY_LENGTH);
                raycastHit2D = Physics2D.Raycast(origin, Vector2.up, Const.RAY_LENGTH);
                destY++;
                mCurrentImage = mImageUp1;
                mCurrentImage2 = mImageUp2;
                break;
        }

        // 重新设置图片
        mRenderer.sprite = mCurrentImage;

        // 先移动旁边的物体
        BaseGameObject neighbor = null;
        if (raycastHit2D.collider != null) {
            neighbor = Get(raycastHit2D.collider.gameObject);
            if (neighbor.type == Type.WALL) {
                return null;
            }
            BaseGameObject box = neighbor.Move(direction, ref moveResult);
            if (moveResult == MoveResult.NO_MOVE) {
                return null;
            }
        }

        // 移动小人
        transform.position = new Vector3(destX, destY, -1);
        if (moveResult <= MoveResult.NORMAL_MOVED) {
            moveResult = MoveResult.NORMAL_MOVED;
        }

        return neighbor;
    }

    /**
     * 开始。 
     */
    private new void Start() {
        base.Start();

        // 初始化成员
        mWaitForDance = new WaitForSeconds(Const.TIMER_INIT);

        // 加载所有图片
        string imageFilePath;

        imageFilePath = string.Format(Const.IMAGE_FILE_PATH, (int)type * 10 + 2);
        mImageLeft1 = Resources.Load<Sprite>(imageFilePath);

        imageFilePath = string.Format(Const.IMAGE_FILE_PATH, (int)type * 10 + 3);
        mImageLeft2 = Resources.Load<Sprite>(imageFilePath);

        imageFilePath = string.Format(Const.IMAGE_FILE_PATH, (int)type * 10 + 4);
        mImageRight1 = Resources.Load<Sprite>(imageFilePath);

        imageFilePath = string.Format(Const.IMAGE_FILE_PATH, (int)type * 10 + 5);
        mImageRight2 = Resources.Load<Sprite>(imageFilePath);

        imageFilePath = string.Format(Const.IMAGE_FILE_PATH, (int)type * 10 + 6);
        mImageUp1 = Resources.Load<Sprite>(imageFilePath);

        imageFilePath = string.Format(Const.IMAGE_FILE_PATH, (int)type * 10 + 7);
        mImageUp2 = Resources.Load<Sprite>(imageFilePath);

        mCurrentImage = mImage;
        mCurrentImage2 = mImage2;

        // 开始跳舞
        StartCoroutine(Dancing());
    }
}
